<script>
export default {
    props: ['modelValue', 'placeholder'],
    emits: ['update:modelValue', 'submit', 'cancel'],
    data() {
        return {}
    },
    computed: {
        value() {
            return this.modelValue.endsWith('\n') ? this.modelValue + '\n' : this.modelValue
        }
    },
    methods: {
        submit() {
            this.$emit('submit', this.modelValue)
        },
        cancel() {
            this.$emit('update:modelValue', '')
            this.$emit('cancel')
        }
    }
}
</script>

<template>
    <div class="textarea">
        <div class="textarea_wapper">
            <div class="textarea_spacer">{{ value }}</div>
            <textarea
                class="textarea_editor"
                :placeholder="placeholder"
                :value="modelValue"
                @input="$emit('update:modelValue', $event.target.value)"></textarea>
        </div>
        <slot>
            <div class="trxtarea_action">
                <button class="textarea_button textarea_submit" @click="submit">发布</button>
                <button class="textarea_button textarea_cancel" @click="cancel">清空</button>
            </div>
        </slot>
    </div>
</template>

<style scoped>
.textarea_wapper {
    position: relative;
}

.textarea_spacer {
    width: 100%;
    padding: 1rem;
    border-radius: 4px;
    resize: none;
    transition: all 0.2s cubic-bezier(0.19, 0.4, 0.17, 0.85);
    margin: 16px 0px;
    min-height: 5rem;
    visibility: hidden;
    font-size: 1rem;
    line-height: 1.5rem;
    white-space: pre-wrap;
}

.textarea_editor {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1rem;
    border-radius: 4px;
    background-color: #f2f2f2;
    resize: none;
    transition: all 0.2s cubic-bezier(0.19, 0.4, 0.17, 0.85);
    outline: none;
    height: 100%;
    overflow-y: hidden;
    box-sizing: border-box;
    font-size: 1rem;
    line-height: 1.5rem;
}

.trxtarea_action {
    display: flex;
    justify-content: flex-end;
    margin: 16px 0px;
}

.textarea_button {
    padding: 5px 16px;
    border-radius: 20px;
    border: 1px solid #818181;
}

.textarea_submit {
    color: #fff;
    background-color: #ec7259;
    border-color: #ec7259;
    margin-right: 1rem;
}

.textarea_submit:hover {
    background-color: #e86045;
}

.textarea_cancel {
    color: #818181;
}

.textarea_cancel:hover {
    background-color: #626262;
}
</style>
